package com.itcodai.dbconnect.config;

import com.itcodai.dbconnect.bean.MyRoutingDataSource;
import com.itcodai.dbconnect.enums.DBTypeEnum;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.jdbc.DataSourceBuilder;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author c-fanqj
 * @date 2023/3/29
 * @apiNote 关于数据源配置，参考SpringBoot官方文档第79章《Data Access》
 * 79 79.1 79.2
 */
//@Configuration
public class DataSourceConfig {

    //@Value("${spring.datasource.master.jdbc-url}")
    //private String master;

    //@Value("${spring.datasource.slavel.jdbc-url}")
    //private String slavel;

    //@Value("${spring.datasource.slave2.jdbc-url}")
    //private String slave2;

    //@Bean
    //@ConfigurationProperties("spring.datasource.master")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    //@Bean
    //@ConfigurationProperties("spring.datasource.slavel")
    public DataSource slave1DataSource() {
        return DataSourceBuilder.create().build();
    }

    //@Bean
    //@ConfigurationProperties("spring.datasource.slave2")
    public DataSource slave2DataSource() {
        return DataSourceBuilder.create().build();
    }

    //@Bean
    public DataSource myRoutingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
                                          @Qualifier("slave1DataSource") DataSource slave1DataSource,
                                          @Qualifier("slave2DataSource") DataSource slave2DataSource
                                          ) {
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DBTypeEnum.MASTER, masterDataSource);
        targetDataSources.put(DBTypeEnum.SLAVE1, slave1DataSource);
        targetDataSources.put(DBTypeEnum.SLAVE2, slave2DataSource);
        MyRoutingDataSource myRoutingDataSource = new MyRoutingDataSource();
        //myRoutingDataSource.setDefaultTargetDataSource(masterDataSource);
        //myRoutingDataSource.setTargetDataSources(targetDataSources);
        //return myRoutingDataSource;
        return null;
    }

}
